package fr.vahren.sriksal.generation;

import fr.vahren.sriksal.generation.DropStrategy.Direction;

public class RiverDrop extends Drop {

	private int inertia;
	private int maxInertia;
	private int downFactor;
	private Direction direction;

	/**
	 * @return the direction
	 */
	public Direction getDirection() {
		if (direction == null) {
			return Direction.NOWHERE;
		} else {
			return direction;
		}
	}

	/**
	 * @param direction
	 *            the direction to set
	 */
	public void setDirection(Direction direction) {
		this.direction = direction;
	}

	/**
	 * @return the downFactor
	 */
	public int getDownFactor() {
		return downFactor;
	}

	/**
	 * @param downFactor
	 *            the downFactor to set
	 */
	public void setDownFactor(int downFactor) {
		this.downFactor = downFactor;
	}

	/**
	 * @return the upFactor
	 */
	public int getUpFactor() {
		return upFactor;
	}

	/**
	 * @param upFactor
	 *            the upFactor to set
	 */
	public void setUpFactor(int upFactor) {
		this.upFactor = upFactor;
	}

	private int upFactor;

	/**
	 * @return the inertia
	 */
	public int getInertia() {
		return inertia;
	}

	/**
	 * @param inertia
	 *            the inertia to set
	 */
	public void setInertia(int inertia) {
		this.inertia = inertia;
	}

	public RiverDrop(Position source, int inertia, int upFactor, int downFactor) {
		super(0);
		setPosition(source);
		this.inertia = inertia;
		this.upFactor = upFactor;
		this.downFactor = downFactor;
		this.setMaxInertia(inertia);
	}

	/**
	 * Gain or lose inertia. Depending on the sign of delta, it is not the same
	 * factor.
	 * 
	 * @param delta
	 */
	public void modifyInertia(int delta) {
		if (delta < 0) {
			inertia = Math.min(maxInertia, inertia - delta * downFactor);
		} else if (delta > 0) {
			inertia = Math.max(0, inertia - delta * upFactor);
		}
	}

	/**
	 * Checks if the drop has enough inertia to go to this height.
	 * 
	 * @param altitude
	 * @return
	 */
	public boolean canGo(int altitude) {
		int delta = altitude - getPosition().getH();
		if (delta > 0) {
			return delta * upFactor <= inertia;
		} else {
			return true;
		}
	}

	public int getMaxInertia() {
		return maxInertia;
	}

	public void setMaxInertia(int maxInertia) {
		this.maxInertia = maxInertia;
	}

}
